package batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 批量发送
 */
public class BatchProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("BatchProducer");
        producer.setNamesrvAddr("");
        producer.start();
        List<Message> messages = new ArrayList<>();
        for (int i = 0;i<100000;i++) {
            Message message = new Message("Simple", "Tags", (i + "_BatchProducer").getBytes(StandardCharsets.UTF_8));
            messages.add(message);
        }
        ListSplitter listSplitter = new ListSplitter(messages);
        int i =0;
        while (listSplitter.hasNext()){
            SendResult sendResult = producer.send(listSplitter.next());
            System.out.println(i++ +"_信息发送成功"+sendResult);

        }
        producer.shutdown();

    }
}

